package net.wickywicky.ps12;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class PS12Activity extends Activity {
	private PS12DataHelper datahelper;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        datahelper = new PS12DataHelper(this);
        setContentView(R.layout.main);
        
        Button bandsbutton = (Button)findViewById(R.id.BandsButton);
        bandsbutton.setOnClickListener(BandButtonListener);
        
        Button updatebutton = (Button)findViewById(R.id.UpdateButton);
        updatebutton.setOnClickListener(UpdateButtonListener);
        
    }
    
    private OnClickListener BandButtonListener = new OnClickListener() {
        public void onClick(View v) {
          // do something when the button is clicked
        	Intent intent = new Intent(v.getContext(), BandListActivity.class);
        	startActivity(intent);
        }
    };
    private OnClickListener UpdateButtonListener = new OnClickListener() {
        public void onClick(View v) {
       		Matcher m;
       		// get band list
        	try {
            	List<String> BandList = new ArrayList<String>();
            	Pattern p = Pattern.compile("^.*<div.+><a.+>(.+)<em>.+");

                URL url = new URL("http://sanmiguelprimaverasound.es/artistas");
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                String str;
                while ((str = in.readLine()) != null) {
                	
                	if (str.contains("artistaSingle")) {
                    	m = p.matcher(str.trim());
                    	if (m.find()) {
                    		
                    		BandList.add(m.group(1).trim());
                    	}                    	
                	}
                }
                in.close();
                datahelper.updateBands(BandList);
            } catch (MalformedURLException e){
            } catch (IOException e) {
                e.printStackTrace();
            }
        	
        	// get schedule
           	try {
           		String artist = "", scenario = "", aux = "";
           		Date date = new Date();
           		
            	Pattern patternday = Pattern.compile("^.*<dt class=\"title\"><a href=\"#\">\\w+ (\\d{1,2}) de (\\w+)<\\/a><\\/dt>.*");
            	Pattern patternartist = Pattern.compile("^.*<td class=\"artista\"><a.*>(.+)<\\/a><\\/td>.*");
            	Pattern patternscenario = Pattern.compile("^.*<td class=\"sala\">(.+)<\\/td>.*");
            	
                URL url = new URL("http://sanmiguelprimaverasound.es/programacion/?lang=ca");
                BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
                String str;
                while ((str = in.readLine()) != null) {
                	aux = str.trim();
                	
                	// day separator found
                	m = patternday.matcher(aux);
                	if (m.find()) {
                		date = new Date(2012 - 1900, (m.group(2).equalsIgnoreCase("maig") ? 4 : 5), Integer.parseInt(m.group(1)),0,0,0);                		
                	}
                	
                	// artist node
                	m = patternartist.matcher(aux);
                	if (m.find()) {
                		artist = m.group(1).trim();
                	}
                	
                	// scenario node
                	m = patternscenario.matcher(aux);
                	if (m.find()) {
                		scenario = m.group(1).trim();
                		Log.v("hola",artist + " " + date + " " + scenario ); 
                		datahelper.addNewGig(artist, date, scenario);
                	}
                }
                in.close();
                
            } catch (MalformedURLException e){
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    
}